package com.wanji.single_point.controller;

import com.wanji.single_point.entity.Count;
import com.wanji.single_point.pojo.CountResult;
import com.wanji.single_point.pojo.PutParam;
import com.wanji.single_point.service.CountService;
import com.wanji.single_point.utils.ResultUtil;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 交通状态感知能力 Count
 * </p>
 *
 * @author xinyingjie
 * @since 2022-07-14
 */
@CrossOrigin
@RestController
@RequestMapping("/count")
public class CountController {
    /**
     * 存放用户信息，key:userId,value:用户创建时间
     */
    private static Map<String, PutParam> user = new ConcurrentHashMap<>();


    @Autowired
    private CountService countService;

    /**
     * 接收okhttp发送工具发送的真值数据，如果有用户访问,将数据传入数据处理逻辑
     *
     * @param jsonParam
     * @return
     */
    @PostMapping("/getReal")
    public ResultUtil getReal(@RequestBody String jsonParam) {

        Count[] bodiesReal = (Count[]) JSONArray.toArray(JSONArray.fromObject(jsonParam), Count.class);

        Count countReal = bodiesReal[0];
//        System.out.println(countReal);
        String realCode = "GW42940400";
        String testCode = "GW42940400";
        Long beginTime = 1560802113491L;
        Long endTime = 1760802113491L;
        PutParam param = countService.putParamer(realCode, testCode, beginTime, endTime);
        String deviceId = "962464";
        String userId = deviceId + realCode + testCode;
        countService.putData(userId, countReal, countReal,param);
        return ResultUtil.success(200,"数据传输成功","ok");
    }

    /**
     * 接收okhttp发送工具发送的测试数据，如果有用户访问,将数据传入数据处理逻辑
     *
     * @param jsonParam
     * @return
     */
    @PostMapping("/getTest")
    public ResultUtil getTest(@RequestBody String jsonParam) {
        Count[] bodiesTest = (Count[]) JSONArray.toArray(JSONArray.fromObject(jsonParam), Count.class);
        Count countTest = bodiesTest[0];
        if (countTest == null) {
            return ResultUtil.fail(400, "数据解析失败");
        }
        if (user.isEmpty()) {
            return ResultUtil.fail(200, "数据传输成功,没有用户访问数据");
        }

        for (String userId : user.keySet()) {
            PutParam param = user.get(userId);
            countService.putData(userId, null, countTest, param);
        }
        return ResultUtil.success(200, "数据传输成功", "ok");

    }


    /**
     * 根据userId获取返回结果
     *
     * @return
     * @throws Exception
     */
    //    @GetMapping("/getResult/{userId}")  @PathVariable String userId
    @PostMapping("/putParam")
    public ResultUtil putParam(@RequestParam(value = "deviceId", defaultValue = "") String deviceId,
                               @RequestParam(value = "realCode", defaultValue = "") String realCode,
                               @RequestParam(value = "testCode", defaultValue = "") String testCode,
                               @RequestParam(value = "beginTime", defaultValue = "") Long beginTime,
                               @RequestParam(value = "endTime", defaultValue = "") Long endTime) {

        PutParam param = countService.putParamer(realCode, testCode, beginTime, endTime);
        String userId = deviceId + realCode + testCode;
        user.put(userId, param);

        return ResultUtil.success(200, "返回结果成功", "ok");
    }


    @PostMapping("/getResult")
    public ResultUtil getResult(@RequestParam(value = "deviceId", defaultValue = "") String deviceId,
                                @RequestParam(value = "realCode", defaultValue = "") String realCode,
                                @RequestParam(value = "testCode", defaultValue = "") String testCode) {

        String userId = deviceId + realCode + testCode;
        CountResult result = countService.getResult(userId);
        if (result == null) {
            return ResultUtil.fail(400, "测试时间到或者参数错误");
        }
        return ResultUtil.success(200, "返回结果成功", result);
    }


    /**
     * 用户推出登录
     *
     * @param userId
     * @return
     */
    @PostMapping("/signout/{userId}")
    public ResultUtil signOut(@PathVariable String userId) {
        user.remove(userId);
        countService.deleteCache(userId);
        return ResultUtil.success(200, "退出成功", "ok");
    }

}
